<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>WebVNC 安全登录</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
        }

        .login-overlay {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0, 0, 0, 0.8);
            display: flex;
            justify-content: center;
            align-items: center;
            z-index: 9999;
        }

        .login-container {
            background: white;
            border-radius: 12px;
            padding: 40px;
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
            width: 90%;
            max-width: 400px;
            position: relative;
        }

        .login-header {
            text-align: center;
            margin-bottom: 30px;
        }

        .login-header h1 {
            color: #333;
            font-size: 24px;
            margin-bottom: 10px;
        }

        .login-header p {
            color: #666;
            font-size: 14px;
        }

        .ssl-notice {
            background: #fff3cd;
            border: 1px solid #ffeaa7;
            border-radius: 6px;
            padding: 12px;
            margin-bottom: 20px;
            font-size: 12px;
            color: #856404;
        }

        .ssl-notice .icon {
            margin-right: 8px;
        }

        .form-group {
            margin-bottom: 20px;
        }

        .form-group label {
            display: block;
            margin-bottom: 5px;
            color: #555;
            font-weight: 500;
        }

        .form-group input {
            width: 100%;
            padding: 12px;
            border: 2px solid #e1e5e9;
            border-radius: 6px;
            font-size: 14px;
            transition: border-color 0.3s ease;
        }

        .form-group input:focus {
            outline: none;
            border-color: #667eea;
        }

        .login-button {
            width: 100%;
            padding: 12px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
            border-radius: 6px;
            font-size: 16px;
            font-weight: 500;
            cursor: pointer;
            transition: transform 0.2s ease;
        }

        .login-button:hover {
            transform: translateY(-1px);
        }

        .login-button:disabled {
            opacity: 0.6;
            cursor: not-allowed;
            transform: none;
        }

        .error-message {
            background: #f8d7da;
            border: 1px solid #f5c6cb;
            color: #721c24;
            padding: 10px;
            border-radius: 6px;
            margin-bottom: 20px;
            font-size: 14px;
            display: none;
        }

        .loading {
            display: none;
            text-align: center;
            margin-top: 20px;
        }

        .loading-spinner {
            border: 2px solid #f3f3f3;
            border-top: 2px solid #667eea;
            border-radius: 50%;
            width: 20px;
            height: 20px;
            animation: spin 1s linear infinite;
            display: inline-block;
            margin-right: 10px;
        }

        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }

        .version-info {
            position: absolute;
            bottom: 10px;
            right: 10px;
            font-size: 10px;
            color: #999;
        }

        .default-hint {
            background: #d1ecf1;
            border: 1px solid #bee5eb;
            border-radius: 6px;
            padding: 10px;
            margin-bottom: 20px;
            font-size: 12px;
            color: #0c5460;
        }
    </style>
</head>
<body>
    <div class="login-overlay" id="loginOverlay">
        <div class="login-container">
            <div class="login-header">
                <h1>🔐 WebVNC 安全登录</h1>
                <p>请输入您的凭据以访问远程桌面</p>
            </div>

            <div class="ssl-notice">
                <span class="icon">🔒</span>
                连接已通过SSL加密保护。如果浏览器显示证书警告，这是正常现象，请点击"继续访问"。
            </div>


            <div class="error-message" id="errorMessage">
                用户名或密码错误，请重试
            </div>

            <form id="loginForm">
                <div class="form-group">
                    <label for="username">用户名</label>
                    <input type="text" id="username" name="username" required autocomplete="username">
                </div>

                <div class="form-group">
                    <label for="password">密码</label>
                    <input type="password" id="password" name="password" required autocomplete="current-password">
                </div>

                <button type="submit" class="login-button" id="loginButton">
                    登录
                </button>
            </form>

            <div class="loading" id="loading">
                <div class="loading-spinner"></div>
                正在验证...
            </div>

            <div class="version-info">
                WebVNC SSL v1.0
            </div>
        </div>
    </div>

    <script>
        // 登录管理器
        class LoginManager {
            constructor() {
                this.form = document.getElementById('loginForm');
                this.errorMessage = document.getElementById('errorMessage');
                this.loading = document.getElementById('loading');
                this.loginButton = document.getElementById('loginButton');
                this.loginOverlay = document.getElementById('loginOverlay');
                
                this.init();
            }

            init() {
                // 绑定表单提交事件
                this.form.addEventListener('submit', this.handleSubmit.bind(this));
                
                // 自动聚焦到用户名输入框
                document.getElementById('username').focus();
                
                // 回车键提交
                this.form.addEventListener('keypress', (e) => {
                    if (e.key === 'Enter') {
                        this.handleSubmit(e);
                    }
                });
            }

            async handleSubmit(event) {
                event.preventDefault();
                
                const username = document.getElementById('username').value.trim();
                const password = document.getElementById('password').value;

                if (!username || !password) {
                    this.showError('请输入用户名和密码');
                    return;
                }

                this.showLoading(true);
                this.hideError();

                try {
                    const response = await this.performLogin(username, password);
                    
                    if (response.success) {
                        // 登录成功，直接跳转到 VNC 页面
                        window.location.href = '/vnc.html?autoconnect=1';
                    } else {
                        this.showError(response.message || '登录失败');
                    }
                } catch (error) {
                    this.showError('网络错误，请重试');
                    console.error('Login error:', error);
                } finally {
                    this.showLoading(false);
                }
            }

            async performLogin(username, password) {
                const response = await fetch('/api/login', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({
                        username: username,
                        password: password
                    })
                });

                if (!response.ok) {
                    throw new Error(`HTTP error! status: ${response.status}`);
                }

                return await response.json();
            }

            showLoading(show) {
                if (show) {
                    this.loading.style.display = 'block';
                    this.loginButton.disabled = true;
                    this.loginButton.textContent = '登录中...';
                } else {
                    this.loading.style.display = 'none';
                    this.loginButton.disabled = false;
                    this.loginButton.textContent = '登录';
                }
            }

            showError(message) {
                this.errorMessage.textContent = message;
                this.errorMessage.style.display = 'block';
                
                // 3秒后自动隐藏错误消息
                setTimeout(() => {
                    this.hideError();
                }, 3000);
            }

            hideError() {
                this.errorMessage.style.display = 'none';
            }

            hideLoginModal() {
                // 淡出效果
                this.loginOverlay.style.opacity = '0';
                this.loginOverlay.style.transition = 'opacity 0.3s ease';
                
                setTimeout(() => {
                    this.loginOverlay.style.display = 'none';
                }, 300);
            }
        }

        // 初始化登录管理器
        document.addEventListener('DOMContentLoaded', () => {
            new LoginManager();
        });

        // 处理来自父窗口的消息
        window.addEventListener('message', (event) => {
            if (event.data.type === 'show_login') {
                document.getElementById('loginOverlay').style.display = 'flex';
            }
        });
    </script>
</body>
</html>
